﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _03CheckBrackets
{
    class Program
    {
        static void Main(string[] args)
        {
            string givenExpression = "(((a+b)/5-d)(";
            Console.WriteLine(givenExpression);
            Console.WriteLine("Are brackets correct? {0}", CountingBrackets(givenExpression));

        }
        static bool CountingBrackets(string str)
        {
            int counter = 0;
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == '(')
                {
                    counter++;
                }
                if (str[i] == ')')
                {
                    counter--;
                }
                if (counter < 0)
                {
                    return false;
                }
            }
            if (str.LastIndexOf('(') > str.LastIndexOf(')'))
            {
                return false;
            }
            if (counter == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
